package com.wf.smartcomm.server.admin.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.wf.smartcomm.server.bean.Feedback;
import com.wf.smartcomm.server.bean.Repair;
import com.wf.smartcomm.server.bean.ThumbImage;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.service.FeedbackService;
import com.wf.smartcomm.server.service.RepairService;
import com.wf.smartcomm.server.util.ConstantUtils;
import com.wf.smartcomm.server.util.StringUtil;


/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-6-8 下午7:55:27 
 * 
 */
@Controller(value="adminRepairAction")
@Scope("prototype")
public class RepairAction extends BaseAction {
	
	@Resource
	private RepairService repairServiceImpl;
	@Resource
	private FeedbackService feedbackServiceImpl;
	
	public void list() throws Exception {
		String aoData = request.getParameter("aoData");
		String sEcho = "";
		String iDisplayStart = "";
		String iDisplayLength = "";
		String sSearch = "";
		
		JSONObject jo = new JSONObject(aoData);
		sEcho = jo.getString("sEcho");
		iDisplayStart = jo.getString("iDisplayStart");
		iDisplayLength = jo.getString("iDisplayLength");
		sSearch = jo.getString("sSearch");
		
		page.setBeginIndex(Integer.parseInt(iDisplayStart));
		page.setPageSize(Integer.parseInt(iDisplayLength));
		User manager = (User) session.getAttribute("loginUser");
		if (!StringUtil.isEmpty(sSearch)) {
			repairServiceImpl.listByKey(manager.getLoginVillage(),sSearch,page);
		}else {
			repairServiceImpl.list(manager.getLoginVillage(),page);
		}
		
		int  initEcho = Integer.parseInt(sEcho)+1;
		int count = page.getTotalCount();
		HashMap<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("iTotalRecords", count);
		dataMap.put("sEcho",initEcho);
		dataMap.put("iTotalDisplayRecords", count);
		dataMap.put("aaData", page.getDataList());
		printJsonData(outGson.toJson(dataMap));
	}
	
/*     */   public String detail() throws Exception {
/*  89 */     String repairId = this.request.getParameter("repairId");
/*  90 */     Repair repair = this.repairServiceImpl.getById(repairId);
/*  91 */     this.feedbackServiceImpl.listByRepair(repair, this.page);
/*     */ 
/* 110 */     this.request.setAttribute("repair", repair);
/*     */ 
/* 112 */     if (this.page.getTotalCount() > 0) {
/* 113 */       List<Feedback> feedbacks = (List<Feedback>) page.getDataList();
/* 122 */       this.request.setAttribute("feedbacks", feedbacks);
/*     */     } 
/*     */ 
/* 127 */     return "detail";
/*     */   }
	
/*     */   public String proFeedback() throws Exception {
/* 131 */    String repairId = request.getParameter("repairId");
/* 133 */     Repair repair = repairServiceImpl.getById(repairId);
				feedbackServiceImpl.listByRepair(repair, page);
				List<Feedback> feedbacks =  (List<Feedback>) page.getDataList();
				if (feedbacks == null) {
					feedbacks = new ArrayList<Feedback>();
				}
				Feedback feedback = new Feedback();
/* 132 */     	String content = this.request.getParameter("content");
				
/* 134 */     User proUser = (User)this.session.getAttribute("loginUser");

/* 137 */     feedback.setUser(proUser);
/* 138 */     feedback.setRepair(repair);
/* 139 */     feedback.setContent(content);
/* 140 */     this.feedbackServiceImpl.save(feedback);
/* 141 */     this.repairServiceImpl.update(repair);
				
				request.setAttribute("feedbacks", feedbacks);
/* 142 */     return "detail";
/*     */   }
}
